﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;

public class CtrlBDConflict
{
    private System.Data.SqlClient.SqlDataAdapter adapter;
    private System.Data.SqlClient.SqlCommand command;
    private System.Data.SqlClient.SqlConnection connection;
    private System.Data.DataSet dataSet;
    private String strConnect;
    private String strSql;

    public CtrlBDConflict()
    {
        strConnect = ConfigurationManager.ConnectionStrings["conexion"].ConnectionString;
        connection = new SqlConnection(strConnect);
    }

    public void addConflict(int iIdRequirements, int iIdConflicted, String sReason)
    {
        connection.Open();
        strSql = "EXEC addConflict @IdRequirements, @IdConflicted, @Reason";
        command = new SqlCommand(strSql, connection);

        SqlParameter p1 = new SqlParameter("@IdRequirements", iIdRequirements);
        SqlParameter p2 = new SqlParameter("@IdConflicted", iIdConflicted);
        SqlParameter p3 = new SqlParameter("@Reason", sReason);

        command.Parameters.Add(p1);
        command.Parameters.Add(p2);
        command.Parameters.Add(p3);

        command.ExecuteNonQuery();
        connection.Close();
    }

    public void deleteConflict(int iIdRequirements, int iIdConflicted)
    {
        connection.Open();
        strSql = "EXEC deleteConflict @IdRequirements, @IdConflicted";
        command = new SqlCommand(strSql, connection);

        SqlParameter p1 = new SqlParameter("@IdRequirements", iIdRequirements);
        SqlParameter p2 = new SqlParameter("@IdConflicted", iIdConflicted);

        command.Parameters.Add(p1);
        command.Parameters.Add(p2);

        command.ExecuteNonQuery();
        connection.Close();
    }

    public Conflicts getConflict(int IdRequirements, int iIdConflicted)
    {
        throw new System.NotImplementedException();
    }

    public void setConflict(int IdRequirements, int iIdConflicted, String sReason)
    {
        throw new System.NotImplementedException();
    }

    public List<Conflicts> getConflicts(int IdRequirements)
    {
        dataSet = new DataSet();

        connection.Open();

        strSql = "EXEC returnConflicts @IdRequirements";
        command = new SqlCommand(strSql, connection);
        SqlParameter p1 = new SqlParameter("@IdRequirements", IdRequirements);
        command.Parameters.Add(p1);
        adapter = new SqlDataAdapter(command);
        adapter.Fill(dataSet, "Conflicts Requeriment");

        connection.Close();

        int cuantity = dataSet.Tables[0].Rows.Count;

        List<Conflicts> list = new List<Conflicts>();

        for (int i = 0; i < cuantity; i++)
        {
            //Breakpoint para cuando el metodo de la base esté hecho.
            list.Add(new Conflicts((int)(dataSet.Tables[0].Rows[i].ItemArray[0]), (dataSet.Tables[0].Rows[i].ItemArray[1]).ToString()));
        }

        return list;
    }
}
